const testURL = 'http://192.168.65.15:8877'
// const testURL = '/api2'

const costURl = 'http://192.168.65.22:8092'
// const costURl = '/api3'

const costURl15 = 'http://192.168.65.15:8085'
// const costURl15 = '/api'

const serviceURL = {
    signIn: `${process.env.VUE_APP_BASE_API}/system/signIn`,
    config: `${process.env.VUE_APP_BASE_API}/system/config`,
    getSystemInfo: `${process.env.VUE_APP_BASE_API}/system/getSystemInfo`,
    getFunction: `${process.env.VUE_APP_BASE_API}/system/getFunctionConfig`,
    test: `${process.env.VUE_APP_BASE_API}/system/test`,
    uploadFile: `${process.env.VUE_APP_BASE_API}/system/uploadFile`,
    uploadResource: `${process.env.VUE_APP_BASE_API}/system/uploadResource`,
        
    serchLoad: `${costURl}/hbase/searchLoad`,
    serchLoadByDay: `${costURl}/hbase/searchLoadByDay`,
    serchMax: `${costURl}/hbase/searchMax`,
    serchMaxByDay: `${costURl}/hbase/searchMaxByDay`,
    searchCDALast7day: `${costURl}/hbase/searchCDALast7day`,
    searchCDA3220P: `${costURl}/hbase/searchCDA3220P`,//3220
    searchChillerCount: `${costURl}/hbase/searchChillerCount`, // 3222
    searchChillerLfAndCop: `${costURl}/hbase/searchChillerLfAndCop`, // 3226
    searchChillerAndCooling: `${costURl}/hbase/searchChillerAndCooling`, // 3228
    searchCoolingHistory: `${costURl}/hbase/searchCoolingHistory`, // 3232
    systemDeviceElectricityConsumption: `${costURl}/hbase/systemDeviceElectricityConsumption`, // 3234
    // 电力大屏实时负荷   
    searchCetNowValue: `${costURl}/hbase/searchCetNowValue`,
    // 电力大屏日耗水量   
    searchWaterValue: `${costURl}/hbase/searchWaterValue`,
  
    search3194: `${costURl15}/search/search3194`, // 3194(日耗水，日耗气，日碳排放，日废水)
    search3180: `${costURl15}/search/search3180`, // 3180(历史耗电量周图表)
    search3182: `${costURl15}/search/search3182`, // 3182(历史耗电量月图表)
    search3184: `${costURl15}/search/search3184`, // 3184(历史耗水量周图表)
    search3188: `${costURl15}/search/search3188`, // 3184(历史耗水量月图表)
    search3186: `${costURl15}/search/search3186`, // 3184(历史耗气量周图表)
    search3190: `${costURl15}/search/search3190`, // 3190(历史耗气量月图表)
    search3196: `${costURl15}/search/search3196`, // 3196(单片能耗周图表)
    search3198: `${costURl15}/search/search3198`, // 3198(单片能耗月图表)
    searchEnthalpyAndTemperature: `${costURl15}/search/searchEnthalpyAndTemperature `, // 3198(单片能耗月图表)
    searchInfoValueBylocation: `${costURl15}/search/searchInfoValueBylocation`, // 3198(单片能耗月图表)
    // 电力大屏日耗气量   
    searchGasValue: `${costURl}/hbase/searchGasValue`,
    // 电力大屏日碳排放量   
    searchCcValue: `${costURl}/hbase/searchCcValue`,
    // 电力大屏日废水排放量  
    searchWasteWaterValue: `${costURl}/hbase/searchWasteWaterValue`,
    // 电力大屏
    test8001: `${testURL}/test8001`,
    test8002: `${testURL}/test8002`,
    //电力大屏日耗电量
    test8003: `${testURL}/test8003`,
    test8004: `${testURL}/test8004`,
    test8005: `${testURL}/test8005`,
    test8006: `${testURL}/test8006`,
    //
    getOpcList: `/opc`,
    getAIResult: `${process.env.VUE_APP_AI_BASE_API}/ai/boot_suggestion`,
    getChillerAI: `${process.env.VUE_APP_AI_BASE_API}/ai/ice_water_params_opt`,
    getChillerResult: `${process.env.VUE_APP_AI_BASE_API}/ai/ice_machine_boot_suggestion`
}

function throwIfMissing(name = throwIfMissing.name) {
    throw new Error(`${name}() Missing parameter`)
}

function newGuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        const r = Math.random() * 16 | 0
        const v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
    })
}
const guid = newGuid()

async function fetchGet(url = throwIfMissing(fetchGet.name), data = '', actionId = '') {
    // console.log('getUrl', url)
    url = `${url}?id=${actionId}&data=${encodeURIComponent(data)}`
    try {
        const response = await fetch(url, {
                method: 'GET',
                mode: 'cors'
            })
            // console.log('getResponse', response)
        if (response.ok) {
            return response.json()
        } else {
            return Promise.reject({
                status: response.status,
                statusText: response.statusText,
                flag: false
            }).json()
        }
    } catch (e) {
        console.log(e)
        return Promise.reject({
            status: e.status,
            statusText: e.statusText,
            flag: false
        })
    }
}
async function fetchGetTest(url = throwIfMissing(fetchGet.name)) {
    // console.log('getUrl', url)
    url = `${url}`
    try {
        const response = await fetch(url, {
            method: 'GET',
            mode: 'cors'
        })
        // console.log('getResponse', response)
        if (response.ok) {
            return response.json()
        } else {
            return Promise.reject({
                status: response.status,
                statusText: response.statusText,
                flag: false
            }).json()
        }
    } catch (e) {
        console.log(e)
        return Promise.reject({
            status: e.status,
            statusText: e.statusText,
            flag: false
        })
    }
}

async function fetchPost(url = throwIfMissing(fetchGet.name), data = '', actionId = 0, file = {}, page = 1, size = 20) {
    const body = new FormData()
    body.append('guid', guid)
    body.append('id', parseInt(actionId, 10))
    body.append('data', data)
    body.append('page', page)
    body.append('size', size)
    for (const key in file) {
        body.append(key, file[key])
    }
    // console.log('postUrl', url)
    try {
        const response = await fetch(url, {
            method: 'POST',
            mode: 'cors',
            body
        })
        // console.log('response', response)
        if (response.ok) {
            return response.json()
        } else {
            return Promise.reject({
                status: response.status,
                statusText: response.statusText,
                flag: false
            })
        }
    } catch (e) {
        console.log(e)
        return Promise.reject({
            status: e.status,
            statusText: e.statusText,
            flag: false
        })
    }
}

async function signIn(id, password) {
    const result = await fetchPost(serviceURL.signIn, JSON.stringify({ id, password }))
    return result
}

async function signOut() {
    const result = await fetchGet(serviceURL.signOut)
    return result
}

async function getSystemInfo() {
    const result = await fetchGet(serviceURL.getSystemInfo)
    return result
}

async function getFunction() {
    console.log(serviceURL.getFunction);
    const result = await fetchGet(serviceURL.getFunction)
    return result
}

async function test() {
    const result = await fetchGet(serviceURL.test)
    return result
}

async function uploadFile(actionId, data, file) {
    const result = await fetchPost(serviceURL.uploadFile, JSON.stringify(data), actionId, file)
    return result
}

async function uploadResource(actionId, data, file) {
    const result = await fetchPost(serviceURL.uploadResource, JSON.stringify(data), actionId, file)
    return result
}

async function fetchByString(actionId, data) {
    const result = await fetchPost(serviceURL.config, data, actionId)
    return result
}

async function fetchByArray(actionId, ...data) {
    return fetchByString(actionId, JSON.stringify(data))
}

async function fetchByJson(actionId, data) {
    return fetchByString(actionId, JSON.stringify(data))
}

async function getSuggestionResult(data) {
    return fetchGet(serviceURL.getAIResult, data)
}
async function serchLoad() {
    return fetchPostTest(serviceURL.serchLoad)
}
async function serchLoadByDay() {
    return fetchPostTest(serviceURL.serchLoadByDay)
}
async function searchCetNowValue() {
    return fetchPostTest(serviceURL.searchCetNowValue)
}
async function searchWaterValue() {
    return fetchPostTest(serviceURL.searchWaterValue)
}
async function search3194() {
    return fetchPostTest(serviceURL.search3194)
}

async function search3180() {
    return fetchPostTest(serviceURL.search3180)
}

async function search3182() {
    return fetchPostTest(serviceURL.search3182)
}

async function search3184() {
    return fetchPostTest(serviceURL.search3184)
}
async function search3188() {
    return fetchPostTest(serviceURL.search3188)
}
async function search3186() {
    return fetchPostTest(serviceURL.search3186)
}

async function search3190() {
    return fetchPostTest(serviceURL.search3190)
}
async function search3196() {
    return fetchPostTest(serviceURL.search3196)
}

async function search3198() {
    return fetchPostTest(serviceURL.search3198)
}

async function searchEnthalpyAndTemperature() {
    return fetchPostTest(serviceURL.searchEnthalpyAndTemperature)
}


async function searchInfoValueBylocation() {
    return fetchPostTest(serviceURL.searchInfoValueBylocation)
}
async function searchGasValue() {
    return fetchPostTest(serviceURL.searchGasValue)
}
async function searchCcValue() {
    return fetchPostTest(serviceURL.searchCcValue)
}
async function searchWasteWaterValue() {
    return fetchPostTest(serviceURL.searchWasteWaterValue)
}
async function serchMax() {
    return fetchPostTest(serviceURL.serchMax)
}
async function serchMaxByDay() {
    return fetchPostTest(serviceURL.serchMaxByDay)
}
async function searchCDALast7day() {
    return fetchPostTest(serviceURL.searchCDALast7day)
}
async function searchCDA3220P() {
    return fetchPostTest(serviceURL.searchCDA3220P)
}
async function searchChillerCount() {
    return fetchPostTest(serviceURL.searchChillerCount)
}
async function searchChillerLfAndCop() {
    return fetchPostTest(serviceURL.searchChillerLfAndCop)
}
async function searchChillerAndCooling() {
    return fetchPostTest(serviceURL.searchChillerAndCooling)
}
async function searchCoolingHistory() {
    return fetchPostTest(serviceURL.searchCoolingHistory)
}
async function systemDeviceElectricityConsumption() {
    return fetchPostTest(serviceURL.systemDeviceElectricityConsumption)
}
async function fetchPostTest(url) {
    const body = new FormData()
        // body.append('guid', guid)
        // body.append('id', parseInt(actionId, 10))
        // body.append('data', data)
        // body.append('page', page)
        // body.append('size', size)
        // for (const key in file) {
        //   body.append(key, file[key])
        // }
    // console.log('postUrl', url)
    try {
        const response = await fetch(url, {
            method: 'POST',
            mode: 'cors',
            body
        })
        // console.log('response', response)
        if (response.ok) {
            return response.json()
        } else {
            return Promise.reject({
                status: response.status,
                statusText: response.statusText,
                flag: false
            })
        }
    } catch (e) {
        console.log(e)
        return Promise.reject({
            status: e.status,
            statusText: e.statusText,
            flag: false
        })
    }
}
async function getChillerAiResult(data) {
    return fetchGet(serviceURL.getChillerAI, data)
}
async function getChillerSuggestionResult(data) {
    return fetchGet(serviceURL.getChillerResult, data)
}
async function test8001() {
    return fetchGetTest(serviceURL.test8001)
}
async function test8002() {
    return fetchGetTest(serviceURL.test8002)
}
async function test8003() {
    return fetchGetTest(serviceURL.test8003)
}
async function test8004() {
    return fetchGetTest(serviceURL.test8004)
}
async function test8005() {
    return fetchGetTest(serviceURL.test8005)
}
async function test8006() {
    return fetchGetTest(serviceURL.test8006)
}
async function test8007() {
    return fetchGetTest(serviceURL.test8007)
}
export default {
    test,
    signIn,
    signOut,
    getSystemInfo,
    getFunction,
    uploadFile,
    uploadResource,
    fetchByArray,
    fetchByJson,
    fetchByString,
    getSuggestionResult,
    serchLoad,
    test8001,
    test8002,
    test8003,
    test8004,
    test8005,
    test8006,
    test8007,
    serchLoadByDay,
    searchCetNowValue,
    searchWaterValue,
    search3194,
    search3180,
    search3182,
    search3184,
    search3188,
    search3186,
    search3190,
    search3196,
    search3198,
    searchEnthalpyAndTemperature,
    searchInfoValueBylocation,
    searchGasValue,
    searchCcValue,
    searchWasteWaterValue,
    serchMax,
    serchMaxByDay,
    searchCDALast7day,
    searchCDA3220P,
    searchChillerCount,
    searchChillerLfAndCop,
    searchChillerAndCooling,
    searchCoolingHistory,
    systemDeviceElectricityConsumption,
    getChillerAiResult,
    getChillerSuggestionResult
}